import java.sql.SQLOutput;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GaoBo
 * Date: 2021-09-02
 * Time: 19:52
 */
class ListNodeD {
    public int data;
    public ListNodeD prev;
    public ListNodeD next;

    public ListNodeD(int data) {
        this.data = data;
    }
}

public class MyLinkedList {
    public ListNodeD head;//头
    public ListNodeD last;//尾

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        if(this.head==null){
            System.out.println("链表为空无法查找");
            return false;
        }
        ListNodeD cur=this.head;
        while(cur!=null){
            if(cur.data==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    //得到单链表的长度
    public int size() {
        ListNodeD cur=this.head;
        int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    public void display() {
        if(this.head==null){
            System.out.println("链表为空无法打印");
            return;
        }
        ListNodeD cur=this.head;
        while(cur!=null){
            System.out.print(cur.data+" ");
            cur=cur.next;
        }
    }
    //头插法
    public void addFirst(int data) {
        ListNodeD Data=new ListNodeD(data);
        if(this.head==null){
            this.head=Data;
            this.last=Data;
        }else{
            this.head.prev=Data;
            Data.next=this.head;
            this.head=Data;
        }
    }
    //尾插法
    public void addLast(int data) {
        ListNodeD Data=new ListNodeD(data);
        if(this.last==null){
            this.last=Data;
            this.head=Data;
        }else{
            this.last.next=Data;
            Data.prev=this.last;
            this.last=Data;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        ListNodeD Data=new ListNodeD(data);
        if(index<0||index>this.size()){
            System.out.println("下标违法，无法插入");
            return;
        }
        if(index==0){
            this.addFirst(data);
            return ;
        }
        if(index==this.size()){
            this.addLast(data);
            return ;
        }
        ListNodeD cur=this.head;
        while(index>0){
            index--;
            cur=cur.next;
        }
        Data.next=cur.next;
        cur.next=Data;
        Data.prev=cur;
        Data.next.prev=Data;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(this.head==null){
            System.out.println("链表为空");
            return ;
        }
        ListNodeD cur=this.head;
        while(cur!=null){
            if(cur.data==key){
                if(cur==this.head&&this.head.next!=null){
                    this.head=this.head.next;
                    this.head.prev=null;
                }else{
                    cur.next.prev=cur.prev;
                    cur.prev.next=cur.next;
                    cur.next=null;
                    cur.prev=null;
                }
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {

    }

    public void clear() {

    }


}
